package net.lateeye.search.thesissearch.acm;

import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

class AcmSRParserForSingleResult extends ParserCallback {

	private static final String DELIM_COMMA = ",";

	private String url_detailedpage = null;
	private String url_pdf = null;
	private LinkedList<String> authors = null;
	private String yearHeld = null;
	private String thesisTitle = null;

	private boolean atAuthors = false;
	private boolean atYearHeld = false;
	private boolean atThesisTitle = false;

	AcmSRParserForSingleResult() {
		super();
	}

	public void handleStartTag(HTML.Tag tag, MutableAttributeSet attr, int pos) {
		int attrCount = attr.getAttributeCount();
		String val_href = (String) attr.getAttribute(HTML.Attribute.HREF);
		String val_class = (String) attr.getAttribute(HTML.Attribute.CLASS);
		String val_target = (String) attr.getAttribute(HTML.Attribute.TARGET);
		String val_nowrap = (String) attr.getAttribute(HTML.Attribute.NOWRAP);
		if (tag.equals(HTML.Tag.A)) {
			if (attrCount == 3 && "medium-text".equals(val_class)
					&& "_self".equals(val_target)) {
				// CASE: URL for detailed page
				//
				// ex.
				// <A
				// HREF="citation.cfm?id=365411.365533&coll=ACM&dl=ACM&CFID=22704101&CFTOKEN=37827144"
				// class="medium-text" target="_self">
				this.url_detailedpage = val_href;
				this.atThesisTitle = true;
			} else if (attrCount == 2 && "_blank".equals(val_target)) {
				// CASE: URL for PDF
				// ex.
				// <A
				// HREF="ft_gateway.cfm?id=365533&type=pdf&coll=ACM&dl=ACM&CFID=22704101&CFTOKEN=37827144"
				// target="_blank">
				this.url_pdf = val_href;
			}
		} else if (tag.equals(HTML.Tag.DIV)) {
			if (attrCount == 1 && "authors".equals(val_class)) {
				// CASE: Authors
				// ex.
				// <div class="authors">Janos Csirik, David S. Johnson, Claire
				// Kenyon</div>
				this.atAuthors = true;
			}
		} else if (tag.equals(HTML.Tag.TD)) {
			if (attrCount == 2 && "small-text".equals(val_class)
					&& val_nowrap != null) {
				// CASE: year the conference(or other) held
				// ex. <td class="small-text" nowrap>
				this.atYearHeld = true;
			}
		}
	}

	public void handleText(char[] data, int pos) {
		String text = new String(data);
		if (this.atAuthors) {
			StringTokenizer st = new StringTokenizer(text, DELIM_COMMA);
			LinkedList<String> ats = new LinkedList<String>();
			while (st.hasMoreTokens()) {
				ats.add(st.nextToken());
			}
			this.authors = ats;
			this.atAuthors = false;
		} else if (this.atYearHeld) {
			this.yearHeld = text;
			this.atYearHeld = false;
		} else if (this.atThesisTitle) {
			this.thesisTitle = text;
			this.atThesisTitle = false;
		}
	}

	String getUrl_detailedpage() {
		return this.url_detailedpage;
	}

	String getUrl_pdf() {
		return this.url_pdf;
	}

	LinkedList<String> getAuthors() {
		return this.authors;
	}

	/**
	 * only year will be returned.
	 * 
	 * @author ISAO Saito <130s@1995.sfc.ne.jp>
	 * @since 2008/04/10
	 * @return
	 */
	int getYearHeld() {
		String ptnStr = "[0-9]{1,}";
		Pattern pattern = Pattern.compile(ptnStr);
		Matcher matcher_resultSize = pattern.matcher(this.yearHeld);
		int n = -1;
		if (matcher_resultSize.find()) {
			n = Integer.parseInt(matcher_resultSize.group());
		}
		return n;
	}

	String getThesisTitle() {
		return this.thesisTitle;
	}
}